package com.leyou.item.web;


import com.leyou.common.dto.PageDTO;
import com.leyou.item.dto.BrandDTO;
import com.leyou.item.service.BrandService;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/brand")
public class BrandController {

    private final BrandService brandService;

    public BrandController(BrandService brandService) {
        this.brandService = brandService;
    }

    /**
     * 根据品牌id查询对应的品牌对象
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public ResponseEntity<BrandDTO> findBrandById(
            @PathVariable("id") Long id) {
        return ResponseEntity.ok(this.brandService.findBrandById(id));

    }

    /**
     * 分页查询品牌
     *
     * @param page 当前页码
     * @param rows 每页大小
     * @param key  查询条件
     * @return 品牌分页结果
     */
    @GetMapping("/page")
    public ResponseEntity<PageDTO<BrandDTO>> queryBrandByPage(
            @RequestParam(value = "page", defaultValue = "1") Integer page,
            @RequestParam(value = "rows", defaultValue = "5") Integer rows,
            @RequestParam(value = "key", required = false) String key) {

        return ResponseEntity.ok(brandService.queryBrandByPage(page, rows, key));

    }

    /**
     * 根据品牌id集合查询对应的品牌集合
     * @param ids
     * @return
     */
    @GetMapping("/list")
    public ResponseEntity<List<BrandDTO>> listBrandByIds(@RequestParam("ids") List<Long> ids) {

        return ResponseEntity.ok(this.brandService.listBrandByIds(ids));
    }

    /**
     * 根据分类查询对应的品牌集合
     * @param cid
     * @return
     */
    @GetMapping("/of/category")
    public ResponseEntity<List<BrandDTO>> listBrandByCategory(
            @RequestParam("id")Long cid){
        return ResponseEntity.ok(this.brandService.listBrandByCategory(cid));
    }

//    /**
//     * 品牌添加
//     * @param name
//     * @param image
//     * @param categoryIds
//     * @param letter
//     * @return
//     */
//    @PostMapping
//    public ResponseEntity<Void> addBrand(
//            @RequestParam("name")String name,
//            @RequestParam("image")String image,
//            @RequestParam("categoryIds")List<Long>categoryIds,
//            @RequestParam("letter")Character letter){
//    }

    /**
     * 直接使用实体类。缺点在于，所有的属性默认都是required=false，这个问题可以通过服务器数据校验解决
     * @param brandDTO
     * @return
     */
    @PostMapping
    public ResponseEntity<Void> addBrand(
            BrandDTO brandDTO){
        this.brandService.addBrand(brandDTO);
        return ResponseEntity.status(HttpStatus.CREATED).build();

    }

//    /**
//     * 品牌修改
//     * @param name
//     * @param image
//     * @param categoryIds
//     * @param letter
//     * @return
//     */
//    @PutMapping
//    public ResponseEntity<Void> updateBrand(
//            @RequestParam("id")Long id,
//            @RequestParam("name")String name,
//            @RequestParam(value = "image",required = false)String image,
//            @RequestParam("categoryIds")List<Long>categoryIds,
//            @RequestParam("letter")Character letter){
//    }

    /**
     * 更新品牌
     * @param brandDTO
     * @return
     */
    @PutMapping
    public ResponseEntity<Void> updateBrand(
            BrandDTO brandDTO){

        this.brandService.updateBrand(brandDTO);
        return ResponseEntity.ok().build();
    }

    /**
     *
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    public ResponseEntity<Void> deleteBrand(
           @PathVariable("id")Long id){
        //删除品牌表
        this.brandService.deleteBrand(id);
        return ResponseEntity.ok().build();

    }
}
